import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

import layout from "@/layout/layout";

import monitorRouter from './modules/monitor';
import systemSettingRouter from './modules/systemSetting';


export const constantRouterMap = [
    {
        path: '/login',
        component: () => import('@/views/login'),
        meta: {title: '登陆', auth: false, noCache: true}
    },
    {
        path: '/404',
        component: () => import('@/views/features/404'),
        meta: {title: '404', auth: false}
    },
    {
        path: '/401',
        component: () => import('@/views/features/401'),
        meta: {title: '401', auth: false}
    },
    {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/features/redirect'),
        meta: {title: '布局', auth: false}
    },
    {
        path: '/', redirect: '/dashboard', component: layout,
        children: [
            {
                path: 'dashboard',
                name: 'dashboard',
                component: () => import('@/views/home'),
                meta: {title: '首页', auth: true, noCache: true}
            },
        ]
    },
    // 实时监控目录
    monitorRouter,
    // 系统设置路由
    systemSettingRouter,
];

const router = new Router({
    mode: 'hash',
    // mode: 'history',
    scrollBehavior: () => ({y: 0}),
    routes: constantRouterMap
})

router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    const auth = to.meta.auth
    if (auth === true) {

        var tokenInfo = sessionStorage.getItem('tokenInfo');

        if (tokenInfo) {
            next()
        } else {
            router.push({path: '/login'}).catch(() => {
            })
        }
    } else {
        next()
    }
});

export default router